package cn.edu.scau.cmi.ema.service;

import java.util.List;
import java.util.Set;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;


import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.Session;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;
import cn.edu.scau.cmi.ema.service.base.CmiEmaService;

import java.io.IOException;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.util.HashSet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import javax.persistence.Query;
import java.util.Iterator;
@Service("Event9ldService")
@Transactional

public class Event9ldServiceImpl extends CmiEmaService implements Event9ldService {
	public Event9ldServiceImpl() {
	}

	@Transactional
	public Event9ld saveEvent9ld(Event9ld event9ld) {

		if (event9ld.getId() == null) {

			Event9ld existingEvent9ld = event9ldDAO.findEvent9ldByPhysicalPrimaryKey(event9ld.getNumber());

			if (isEquals(event9ld, existingEvent9ld)) {
				return existingEvent9ld;
			}

			event9ld = event9ldDAO.store(event9ld);
			event9ldDAO.flush();

		} else {
			Event9ld existingEvent9ld = event9ldDAO.findEvent9ldByPrimaryKey(event9ld.getId());

			event9ld = event9ldDAO.store(event9ld);
			event9ldDAO.flush();
		}

		return event9ld;
	}

	public void deleteEvent9ld(Event9ld event9ld) {
		event9ldDAO.remove(event9ld);
		event9ldDAO.flush();
	}

	@Transactional
	public void deleteEvent9lds(Set<Integer> deleteEvent9ldsId) {
		Query query = event9ldDAO.createNamedQuery("deleteEvent9ldsById", -1, -1);
		query.setParameter(1, deleteEvent9ldsId);
		query.executeUpdate();
	}

	@Transactional
	public Set<Event9ld> loadEvent9lds() {
		return event9ldDAO.findAllEvent9lds();

	}

	@Transactional
	public Set<Event9ld> loadReferenceEvent9lds(Set<Event9ld> event9lds) {
		Iterator<Event9ld> event9ldsIterator = event9lds.iterator();
		Set<Event9ld> referencedEvent9lds = new HashSet<>();
		while (event9ldsIterator.hasNext()) {
			Event9ld currentEvent9ld = event9ldsIterator.next();

		}
		return referencedEvent9lds;
	}

	public Set<Event9ld> loadEvent9ldsByIdSet(Set<Integer> event9ldIdSet) {

		Iterator<Integer> event9ldIdIterator = event9ldIdSet.iterator();
		Set<Event9ld> event9ldSet = new HashSet<>();
		while (event9ldIdIterator.hasNext()) {
			Integer id = event9ldIdIterator.next();
			Event9ld currentEvent9ld = event9ldDAO.findEvent9ldById(id);
			event9ldSet.add(currentEvent9ld);
		}
		return event9ldSet;
	}

	@Transactional
	public List<Event9ld> findAllEvent9lds(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Event9ld>(event9ldDAO.findAllEvent9lds(startResult, maxRows));
	}

	@Transactional
	public Event9ld findEvent9ldByPrimaryKey(Integer id) {
		return event9ldDAO.findEvent9ldByPrimaryKey(id);
	}

	@Transactional
	public Integer countEvent9lds() {
		return ((Long) event9ldDAO.createQuerySingleResult("select count(o) from Event9ld o").getSingleResult())
				.intValue();
	}

	@Transactional
	public Integer countEvent9lds(CmiPagination page) {
		String sql = CmiSqlUtil.createCountHql(page, Event9ld.class);
		return ((Long) event9ldDAO.createQuerySingleResult(sql).getSingleResult()).intValue();
	}

	/**
	 * search for name with ajax
	 * 
	 */
	@Transactional
	public Integer countEvent9ldsByName(String queryName) {
		Query query = event9ldDAO.createNamedQuery("countEvent9ldsByName", -1, -1);
		query.setParameter(1, queryName);
		return new Long((long) query.getSingleResult()).intValue();
	}

	@Transactional
	public Integer countEvent9ldsByDetachedCriteria(CmiPagination page) {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Event9ld.class);
		Session session = entityManager.unwrap(Session.class);
		detachedCriteria.setProjection(Projections.rowCount());
		return new Long((long) detachedCriteria.getExecutableCriteria(session).uniqueResult()).intValue();
	}

	public Map<String, ?> loadEvent9ldObjectReferencedSet(Event9ld event9ld) {
		HashMap<String, Object> objectReferencedSetMap = new HashMap<>();

		return objectReferencedSetMap;
	}

	public Map<String, ?> loadEvent9ldClassReferencedSet(Event9ld event9ld) {
		HashMap<String, Object> classReferencedSetMap = new HashMap<>();

		return classReferencedSetMap;
	}

	public boolean isEquals(Event9ld event9ld, Object obj) {
		if (obj == event9ld)
			return true;
		if (!(obj instanceof Event9ld))
			return false;
		Event9ld comparedEvent9ld = (Event9ld) obj;
		if (!CheckPropertyEqual(event9ld.getNumber(), comparedEvent9ld.getNumber())) {
			return false;
		}
		return true;
	}

	public static <T> boolean CheckPropertyEqual(T originProperty, T comparedProperty) {
		// 基本类型经过参数传递后会自动装箱
		String[] className = originProperty.getClass().getName().split("\\.");
		if (className[0].equals("java") && className[1].equals("math")) {
			return ((Comparable) originProperty).compareTo(comparedProperty) == 0 ? true : false;
		} else {
			return originProperty.equals(comparedProperty);
		}
	}
}

